<template>
	<!-- <view>goodsListllp</view> -->
	<view>
		<view class="goods-list">
			<view v-for="(item, index) in goodsList" :key="index" @click="gotoDetail(item)"><my-goods :goods="item"></my-goods></view>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			// 请求参数对象
			queryObj: {
				// 查询关键词
				query: '',
				// 商品分类Id
				cid: '',
				// 页码值
				pagenum: 1,
				// 每页显示多少条数据
				pagesize: 10
			},
			goodsList: [],
			total: 0,
			isLoading: false
		};
	},
	onLoad(options) {
		console.log(options);
		// 将页面参数转存到 this.queryObj 对象中
		this.queryObj.query = options.query || '';
		this.queryObj.cid = options.goods_id || '';
		this.getGoodsList();
	},
	onReachBottom() {
		// console.log('bottom'); 1 一个节流阀 2 判断数据加载是否完毕
		if (this.queryObj.pagenum * this.queryObj.pagesize >= this.total) {
			return uni.$showMsg('数据加载完毕');
		}
		if (this.isLoading) return;
		this.queryObj.pagenum++;
		this.getGoodsList();
	},
	onPullDownRefresh() {
		// console.log('下拉刷新');
		this.goodsList = [];
		this.total = 0;
		this.queryObj.pagenum = 1;
		this.isLoading = false;
		this.getGoodsList(() => {
			uni.stopPullDownRefresh();
		});
	},
	methods: {
		async getGoodsList(cb) {
			this.isLoading = true;
			const { data: res } = await uni.$http.get('/api/public/v1/goods/search', this.queryObj);
			console.log(res);
			cb && cb();
			if (res.meta.status !== 200) {
				uni.$showMsg();
				this.goodsList = [];
				return;
			}
			this.isLoading = false;
			this.goodsList = [...this.goodsList, ...res.message.goods];
			this.total = res.message.total;
		},
		/* 跳转详情页面 */
		gotoDetail(item){
			uni.navigateTo({
				url:'/subpkg/goods_detail/goods_detail?goods_id='+item.goods_id
			})
		}
	}
};
</script>

<style lang="scss"></style>
